﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace APC_Application
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const int CanvasWidth = 1366;
        private const int CanvasHeight = 768;

        private const int NumberOfRectanglesHorzintal = 10;
        private const int NumberOfRectanglesVertical = 5;

        private int _numberOfRetangles;
        private int _progression;

        private readonly List<Rectangle> _rectangleList = new List<Rectangle>();
        private readonly List<string> _motivationalMessagesList = new List<string>
        {
            "Det går rigtig godt!",
            "Weee du er flyvende :)",
            "Sådan! Det kører!",
            "Du er der snart!",
            "Wup wup",
            "For hun er en af vor egne, for hun er en af vor eeeeg-neeee!",
            "Hva' så Armstrong, kører det?",
            "Du er da vist ikke helt ren i kanten? :)",
            "What?!?! Tour de france, here we come!"
        };

        private readonly int _messageInterval;

        public MainWindow()
        {
            InitializeComponent();

            _numberOfRetangles = NumberOfRectanglesHorzintal * NumberOfRectanglesVertical;
            _messageInterval = (int)Math.Floor(_numberOfRetangles / (double)_motivationalMessagesList.Count);

            SimulationSlider.Maximum = _numberOfRetangles;

            var rectangleWidth = Math.Ceiling(CanvasWidth / (double)NumberOfRectanglesHorzintal);
            var rectangleHeight = Math.Ceiling(CanvasHeight / (double)NumberOfRectanglesVertical);

            var tmpRectangleList = new List<Rectangle>();

            var colorBrushList = new List<SolidColorBrush>
            {
                Brushes.CadetBlue,
                Brushes.Brown,
                Brushes.Chocolate,
                Brushes.BlueViolet,
                Brushes.CornflowerBlue,
                Brushes.DarkBlue,
                Brushes.DarkGreen,
                Brushes.Crimson,
                Brushes.Chartreuse,
                Brushes.DarkSeaGreen,
                Brushes.DeepPink,
                Brushes.Coral,
                Brushes.DimGray,
                Brushes.Black,
            };

            var randomNumber = new Random();

            for (var i = 0; i < NumberOfRectanglesHorzintal; i++)
            {
                for (var j = 0; j < NumberOfRectanglesVertical; j++)
                {
                    var posX = i * rectangleWidth;
                    var posY = j * rectangleHeight;

                    var rect = new Rectangle
                    {
                        Fill = colorBrushList.ElementAt(randomNumber.Next(colorBrushList.Count)),
                        Width = rectangleWidth,
                        Height = rectangleHeight
                    };

                    tmpRectangleList.Add(rect);

                    Canvas.SetLeft(rect, posX);
                    Canvas.SetTop(rect, posY);

                    PhotoCanvas.Children.Add(rect);
                }
            }

            _rectangleList = tmpRectangleList.OrderBy(item => randomNumber.Next()).ToList();
        }

        private bool HideNextRectangle()
        {
            if (_rectangleList.Count == 0)
            {
                MotivationalMessageBorder.Visibility = Visibility.Hidden;
                return true;
            }

            var rect = _rectangleList.First();
            rect.Visibility = Visibility.Hidden;
            _rectangleList.Remove(rect);

            _progression++;
            if (_messageInterval == _progression && _motivationalMessagesList.Count > 0)
            {
                MotivationalMessageBorder.Visibility = Visibility.Visible;
                var msg = _motivationalMessagesList.First();
                MotivationalMessageTextBlock.Text = msg;
                _motivationalMessagesList.Remove(msg);
                _progression = 0;
            }

            return false;
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (HideNextRectangle())
                FinishMessage.Visibility = Visibility.Visible;
        }
    }
}
